/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: signal_manager.cpp
 *
 * Purpose: signal manager service implementation for PandaOS
 *
 * Developer:
 *   wen.gu , 2020-09-02
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#define LOG_TAG "SMSV"
#include "signal_manager.h"

#include "panda/core/utils.h"
#include "panda/core/log.h"
namespace panda
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define SIG_MANAGER_SRV_NAME "signal_manager"
#define EMIT_SIG_METHOD "emit_signal"
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
SignalManager::SignalManager()
{ 
    mSrv = new ServiceProvider(SIG_MANAGER_SRV_NAME);
    /** todo something */
}

SignalManager::~SignalManager()
{
    /** todo something */
    delete mSrv;
}

void SignalManager::run()
{
    mSrv->registerMethod(EMIT_SIG_METHOD, [this](panda::com::ServiceRequestHandlePtr srhdl, const std::string& method, const panda::core::Value& param)
    {
        Value reply;
        if (param.isMember("sig_name"))
        {
            std::string sigName = param["sig_name"].asString();
            Value val;
            if (param.isMember("data"))
            {
                val = param["data"];
            }
            mSrv->emitSignal(sigName, val);
            panda::core::Error2Json(PandaErrc::OK, reply);
        }
        else
        {
            panda::core::Error2Json(PandaErrc::BadParameter, reply);
        }
        mSrv->sendResponse(srhdl, reply);
    });

    PandaErrc ret = mSrv->start();

    if (ret != PandaErrc::OK)
    {
        LOGE("start service: %s, failed(%s)\n", SIG_MANAGER_SRV_NAME, panda::core::ErrorStr(ret));
    }

    while (1)
    {
        panda::core::SleepMs(100);
    }
}

} /** namespace panda */

int main(int argc, char* argv[])
{
    panda::SignalManager sm;

    sm.run();

    return 0;
}


